package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.gt3.entity.JcAjtzztbEntity;
import cn.com.css.taxInspection.gt3.entity.JcAjztSlytzEntity;
import cn.com.css.taxInspection.gt3.entity.JcAyxxDebugJgryEntity;
import cn.com.css.taxInspection.gt3.mapper.JcAJjtzztbMapper;
import cn.com.css.taxInspection.gt3.mapper.JcAJtzSlytzMapper;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.gt3.service.JcAjtzSlytzService;
import cn.com.css.taxInspection.gt3.service.JcAjtzztbService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class JcAjtzSlytzServiceImpl extends ServiceImpl<JcAJtzSlytzMapper, JcAjztSlytzEntity> implements JcAjtzSlytzService {

    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private JcAjtzztbService jcAjtzztbService;
    @Resource
    private DmGySwryService dmGySwryService;

    //审理员-分页查询
    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public IPage<JcAjztSlytzEntity> pageInfo(JcAjztSlytzEntity jcAjztSlytzEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Page page = new Page(jcAjztSlytzEntity.getPage(), jcAjztSlytzEntity.getLimit());
        LambdaQueryWrapper<JcAjztSlytzEntity> queryWrapper = Wrappers.lambdaQuery();
        String deptIdPrefix = httpSessionService.getCurrentDeptId().substring(0, 9); // 默认前缀（9位）
        queryWrapper.like(JcAjztSlytzEntity::getJcbmDm, deptIdPrefix);
        //审理类型 0 未调整  1 调整
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getSllx())) {
            queryWrapper.eq(JcAjztSlytzEntity::getSllx, jcAjztSlytzEntity.getSllx());
        }
        //检查案件编号
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getJcajbh())) {
            queryWrapper.like(JcAjztSlytzEntity::getJcajbh, jcAjztSlytzEntity.getJcajbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getNsrmc())) {
            queryWrapper.like(JcAjztSlytzEntity::getNsrmc, jcAjztSlytzEntity.getNsrmc());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getNsrsbh())) {
            queryWrapper.like(JcAjztSlytzEntity::getNsrsbh, jcAjztSlytzEntity.getNsrsbh());
        }
        //案件状态
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getAjztmc())) {
            queryWrapper.eq(JcAjztSlytzEntity::getAjztmc, jcAjztSlytzEntity.getAjztmc());
        }
        //案件状态
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getJcxm())) {
            queryWrapper.eq(JcAjztSlytzEntity::getJcxm, jcAjztSlytzEntity.getJcxm());
        }
        //立案日期
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getLasjq())) {
            queryWrapper.between(JcAjztSlytzEntity::getLarq, sdf.parse(jcAjztSlytzEntity.getLasjq()), sdf.parse(jcAjztSlytzEntity.getLasjz()));
        }
        queryWrapper.orderByDesc(JcAjztSlytzEntity::getJcajbh);
        IPage<JcAjztSlytzEntity> iPage = this.page(page, queryWrapper);
        List<JcAjztSlytzEntity> records = iPage.getRecords();
        setSwryxm(records);
        return iPage;
    }

    //审理员不分页查询
    @Override
    public List<JcAjztSlytzEntity> queryAjtzSlytzList(JcAjztSlytzEntity jcAjztSlytzEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<JcAjztSlytzEntity> queryWrapper = Wrappers.lambdaQuery();
        String deptIdPrefix = httpSessionService.getCurrentDeptId().substring(0, 9); // 默认前缀（9位）
        queryWrapper.like(JcAjztSlytzEntity::getJcbmDm, deptIdPrefix);
        //审理类型 0 未调整  1 调整
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getSllx())) {
            queryWrapper.eq(JcAjztSlytzEntity::getSllx, jcAjztSlytzEntity.getSllx());
        }
        //检查案件编号
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getJcajbh())) {
            queryWrapper.like(JcAjztSlytzEntity::getJcajbh, jcAjztSlytzEntity.getJcajbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getNsrmc())) {
            queryWrapper.like(JcAjztSlytzEntity::getNsrmc, jcAjztSlytzEntity.getNsrmc());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getNsrsbh())) {
            queryWrapper.like(JcAjztSlytzEntity::getNsrsbh, jcAjztSlytzEntity.getNsrsbh());
        }
        //案件状态
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getAjztmc())) {
            queryWrapper.eq(JcAjztSlytzEntity::getAjztmc, jcAjztSlytzEntity.getAjztmc());
        }
        //案件状态
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getJcxm())) {
            queryWrapper.eq(JcAjztSlytzEntity::getJcxm, jcAjztSlytzEntity.getJcxm());
        }
        //立案日期
        if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getLasjq())) {
            queryWrapper.between(JcAjztSlytzEntity::getLarq, sdf.parse(jcAjztSlytzEntity.getLasjq()), sdf.parse(jcAjztSlytzEntity.getLasjz()));
        }
        queryWrapper.orderByAsc(JcAjztSlytzEntity::getLarq);

        List<JcAjztSlytzEntity> jcAjztSlytzEntityList = this.list(queryWrapper);
        setSwryxm(jcAjztSlytzEntityList);
        return jcAjztSlytzEntityList;
    }

    //设置税务人员姓名
    public void setSwryxm(List<JcAjztSlytzEntity> jcAjztSlytzEntities) {
        for (JcAjztSlytzEntity jcAjztSlytzEntity : jcAjztSlytzEntities) {
            if (TsBqUtils.isNotNull(jcAjztSlytzEntity.getJcry())) {
                String[] jcryList = jcAjztSlytzEntity.getJcry().split(",");
                // 用StringBuilder拼接结果
                StringBuilder combinedResult = new StringBuilder();
                for (int j = 0; j < jcryList.length; j++) {
                    // 获取当前代码
                    String code = jcryList[j];
                    // 查询对应名称
                    if (code.length() > 11) {
                        return;
                    }
                    String name = dmGySwryService.getSwry(code).getSwryxm();
                    // 拼接格式为 "代码|名称"
                    combinedResult.append(code).append("|").append(name);
                    // 如果不是最后一个元素，加上逗号分隔
                    if (j < jcryList.length - 1) {
                        combinedResult.append(",");
                    }
                    // 将拼接后的字符串赋值回实体对象
                    jcAjztSlytzEntity.setJcry(combinedResult.toString());
                }
            }
        }
    }
}
